﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class SoHieuTaiKhoanBO : Csla.BusinessBase<SoHieuTaiKhoanBO>
    {
        #region Business Properties
        public string SortStr = "00000";

        private int? _TaiKhoanKTID = null;
        public int? TaiKhoanKTID
        {
            get { return this._TaiKhoanKTID; }
            set { this._TaiKhoanKTID = value; }
        }

        private int? _SoHieu1 = null;
        public int? SoHieu1
        {
            get { return this._SoHieu1; }
            set { this._SoHieu1 = value; }
        }

        private int? _SoHieu2 = null;
        public int? SoHieu2
        {
            get { return this._SoHieu2; }
            set { this._SoHieu2 = value; }
        }

        private int? _SoHieu3 = null;
        public int? SoHieu3
        {
            get { return this._SoHieu3; }
            set { this._SoHieu3 = value; }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; }
        }

        private string _StrSoHieu = null;
        public string StrSoHieu
        {
            get
            {
                if (this._SoHieu3 != null)
                    return this._SoHieu3.Value.ToString("00000");
                else
                    if (this._SoHieu2 != null)
                        return this._SoHieu2.Value.ToString("0000");
                    else
                        if (this._SoHieu1 != null)
                            return this._SoHieu1.Value.ToString("000");
                        else
                            return null;
            }
            set
            {
                this._StrSoHieu = value;
            }
        }

        protected override object GetIdValue()
        {
            return _TaiKhoanKTID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newTaiKhoanKTID = -1;
        private SoHieuTaiKhoanBO()
        {
            _TaiKhoanKTID = newTaiKhoanKTID--;
        }

        public SoHieuTaiKhoanBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public SoHieuTaiKhoanBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static SoHieuTaiKhoanBO NewRootSoHieuTaiKhoanBO()
        {
            return DataPortal.Create<SoHieuTaiKhoanBO>(new RootCriteria());
        }

        internal static SoHieuTaiKhoanBO NewChildSoHieuTaiKhoanBO()
        {
            return DataPortal.Create<SoHieuTaiKhoanBO>(new ChildCriteria());
        }

        public static SoHieuTaiKhoanBO GetRootSoHieuTaiKhoanBO(int _TaiKhoanKTID)
        {
            return DataPortal.Fetch<SoHieuTaiKhoanBO>(new RootCriteria(_TaiKhoanKTID));
        }

        public static SoHieuTaiKhoanBO GetRootSoHieuTaiKhoanBOByWhere(string where)
        {
            return DataPortal.Fetch<SoHieuTaiKhoanBO>(where);
        }

        internal static SoHieuTaiKhoanBO GetChildSoHieuTaiKhoanBO(SqlDataReader dataReader)
        {
            return new SoHieuTaiKhoanBO(dataReader);
        }

        public static void DeleteSoHieuTaiKhoanBO(int _TaiKhoanKTID)
        {
            DataPortal.Delete(new RootCriteria(_TaiKhoanKTID));
        }

        public static void DeleteSoHieuTaiKhoanBO(int _TaiKhoanKTID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_TaiKhoanKTID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int TaiKhoanKTID;
            public bool directDelete = false;
            public RootCriteria(int _TaiKhoanKTID)
            {
                this.TaiKhoanKTID = _TaiKhoanKTID;
                this.directDelete = false;
            }

            public RootCriteria(int _TaiKhoanKTID, bool directDelete)
            {
                this.TaiKhoanKTID = _TaiKhoanKTID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int TaiKhoanKTID;
            public bool directDelete = false;
            public ChildCriteria(int _TaiKhoanKTID)
            {
                this.TaiKhoanKTID = _TaiKhoanKTID;
                this.directDelete = false;
            }

            public ChildCriteria(int _TaiKhoanKTID, bool directDelete)
            {
                this.TaiKhoanKTID = _TaiKhoanKTID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoHieuTaiKhoanBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[TaiKhoanKTID] = " + criteria.TaiKhoanKTID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoHieuTaiKhoanBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["TaiKhoanKTID"] is DBNull))
                this._TaiKhoanKTID = (int)dataReader["TaiKhoanKTID"];
            if (!(dataReader["SoHieu1"] is DBNull))
                this._SoHieu1 = (int)dataReader["SoHieu1"];
            if (!(dataReader["SoHieu2"] is DBNull))
                this._SoHieu2 = (int)dataReader["SoHieu2"];
            if (!(dataReader["SoHieu3"] is DBNull))
                this._SoHieu3 = (int)dataReader["SoHieu3"];
            if (!(dataReader["Ten"] is DBNull))
                this._Ten = (string)dataReader["Ten"];

            this.SortStr = "";
            if (this._SoHieu1 == null)
                this.SortStr += "000";
            else
                this.SortStr += this._SoHieu1.Value.ToString("000");

            if (this._SoHieu2 == null)
                this.SortStr += "0000";
            else
                this.SortStr += this._SoHieu2.Value.ToString("0000");

            if (this._SoHieu3 == null)
                this.SortStr += "00000";
            else
                this.SortStr += this._SoHieu3.Value.ToString("00000");
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_TaiKhoanKTID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_TaiKhoanKTID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
